package movie.utility;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Slavko
 */
public class JsfUtil {

	public static void failValidation() {
		FacesContext.getCurrentInstance().validationFailed();
	}

	public static void addInfoMessage(String message) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Info", message));
	}

	public static void addWarningMessage(String message) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Warning", message));
	}

	public static void addValidationFailedForComponent(String idComponent, String message) {
		FacesContext.getCurrentInstance().addMessage(idComponent, new FacesMessage(FacesMessage.SEVERITY_ERROR, null, message));
		failValidation();
	}

	public static void addSaveSuccesfullMessage() {
		addInfoMessage("Save succesfull");
	}

	public static String getAttributeStringFromComponentAttributes(ActionEvent event, String attributeName) {
		Object o = event.getComponent().getAttributes().get(attributeName);
		return o == null ? null : o.toString();
	}

	public static Integer getAttributeIntegerFromComponentAttributes(ActionEvent event, String attributeName) {
		String s = getAttributeStringFromComponentAttributes(event, attributeName);
		return Utility.isStringEmptyOrNull(s) ? null : new Integer(s);
	}

	public static void forwardToHomePage() {
		FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(
				FacesContext.getCurrentInstance(), null, "home?faces-redirect=true");
	}
}
